﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Enterprise.Utility
{
    public static  class ObjectExtend {
        
        /// <summary>
        /// 从对象中取得字符串
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static string GetString(this object obj, string defaultValue) {
            return obj == null ? defaultValue : obj== DBNull.Value ? defaultValue : obj.ToString();
        }

        /// <summary>
        /// 从对象中取得int数据
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static int GetInt(this object obj, int defaultValue) {
            return obj == null ? defaultValue : obj == DBNull.Value ? defaultValue : Convert.ToInt32 (obj);
        }
        /// <summary>
        /// 从对象中取得int数据
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="isCatch">是否处理异常</param>
        /// <returns></returns>
        public static int GetInt(this object obj, int defaultValue,bool isCatch) {
            try {
                if (obj == null)
                    return defaultValue;
                else if (string.IsNullOrEmpty(obj.ToString())) return defaultValue;
                return Convert.ToInt32(obj);
            } catch(Exception ex) {
                if(isCatch) {
                    throw ex;
                } else {
                    return defaultValue;
                }
            }
        }

        /// <summary>
        /// 从对象中取得long数据
        /// </summary>
        /// <param name="obj">对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <param name="isCatch">是否处理异常</param>
        /// <returns></returns>
        public static long GetLong(this object obj, long defaultValue, bool isCatch) {
            try {
                return Convert.ToInt64(obj);
            } catch (Exception ex) {
                if (isCatch) {
                    throw ex;
                } else {
                    return defaultValue;
                }
            }
        }
        /// <summary>
        /// 从对象中取得时间
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static DateTime GetDateTime(this object obj, DateTime  defaultValue) {
            return obj == null ? defaultValue : obj == DBNull.Value ? defaultValue : Convert.ToDateTime(obj);
        }
        /// <summary>
        /// 从对象中取得时间
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static DateTime GetDateTime(this object obj, DateTime defaultValue,bool throwCache) {
            try {
                return obj == null ? defaultValue : obj == DBNull.Value ? defaultValue : Convert.ToDateTime(obj);
            } catch(Exception ex) {
                if (!throwCache) return defaultValue;
                throw ex;
            }
        }

        /// <summary>
        /// 判断对象是否是空
        /// </summary>
        /// <param name="obj">要判断的对象</param>
        /// <returns></returns>
        public static bool isDbNullOrNull(this object obj){
            return obj == null || obj == DBNull.Value ;
        }

        /// <summary>
        /// 从对象中取得int数据 取不到时,返回 null
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns></returns>
        public static int? GetInt(this object obj) {
            if (obj.isDbNullOrNull()) {
                return null;
            } else {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 从对象中取得时间数据,取不到时,返回 null
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns></returns>
        public static DateTime? GetDateTime(this object obj) {
            if (obj.isDbNullOrNull()) {
                return null;
            } else {
                return Convert.ToDateTime(obj);
            }
        }

        /// <summary>
        /// 从对象中取得bool数据
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static bool  GetBoolean(this object obj, bool defaultValue) {
            return obj == null ? defaultValue : obj == DBNull.Value ? defaultValue : Convert.ToBoolean(obj);
        }

        /// <summary>
        /// 如果对象为空,取默认值 
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defaultValue">为空时的默认值</param>
        /// <returns></returns>
        public static object GetDefault(this object obj, object defaultValue) {
            return obj == null ? defaultValue : obj == DBNull.Value ? defaultValue : obj;
        }

        /// <summary>
        /// 序列化对象为字符串
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns></returns>
        public static string SerializeToString(this object obj) {
            IFormatter formatter = new BinaryFormatter();
            string result = string.Empty;
            using (MemoryStream stream = new MemoryStream()) {
                formatter.Serialize(stream, obj);

                byte[] byt = new byte[stream.Length];
                byt = stream.ToArray();
                
                result = Convert.ToBase64String(byt);
                stream.Flush();
            }
            return result;
        }
    }
}
